package com.dlc.shop.poly.platform.controller.biz;


import com.linchuang.seneca.constants.PolyMallConstants;
import com.linchuang.seneca.enums.PolyErrorCodeEnum;
import com.linchuang.seneca.exception.PolyMallException;
import com.linchuang.seneca.request.PolyMallCommonRequest;
import com.linchuang.seneca.response.PolyMallCommonResponse;
import com.linchuang.seneca.utils.ExceptionUtils;
import com.linchuang.seneca.utils.PolyMallResponseManage;
import com.dlc.shop.poly.common.service.PolyMallOrderService;
import com.dlc.shop.poly.common.service.PolyMallService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class PolyMallBiz {

    @Autowired
    private PolyMallService service;

    @Autowired
    private PolyMallOrderService polyMallOrderService;

    /**
     * 菠萝派商城统一入口
     *
     * @param request
     * @param servletRequest
     * @return
     */
    public PolyMallCommonResponse action(PolyMallCommonRequest request, HttpServletRequest servletRequest) {
        try {
            log.info("[PolyMallBiz][action]菠萝派接收参数：{}", request.toString());
            service.checkRequestAndSign(request);

            switch (request.getMethod()) {
                case PolyMallConstants.Method.ORDER:
                    //商品下单
                    return polyMallOrderService.queryOrders(request);

                case PolyMallConstants.Method.CHECK_REFUND:
                    //商品退款
                    PolyMallCommonResponse polyMallCommonResponse = polyMallOrderService.checkRefund(request);
                    log.info("检测售后接口返回："+polyMallCommonResponse.toString());
                    return polyMallCommonResponse;

                case PolyMallConstants.Method.SEND:
                    //商品发货
                    return polyMallOrderService.send(request);

                case PolyMallConstants.Method.DOWNLOAD_PRODUCT:
                    //商品下载
                    return polyMallOrderService.downloadProduct(request);
                case PolyMallConstants.Method.SYNC_STOCK:
                    //商品库存同步
                   return polyMallOrderService.syncStock(request) ;
                case PolyMallConstants.Method.QUERY_WARE_HOUSE:
                    //仓库查询接口
                   return polyMallOrderService.queryWareHouse(request) ;
                case PolyMallConstants.Method.REFUND:
                    //退货退款单下载接口（Differ.JH.Business.GetRefund）
                   return polyMallOrderService.getRefund(request) ;
            }
        } catch (PolyMallException e) {

            log.error("[PolyMallBiz][action]菠萝派mall业务异常：{}，请求：{}", e.getDesc(), request.toString());
            log.error("[PolyMallBiz][action]菠萝派mall业务异常" + ExceptionUtils.getRunException(e));
            return PolyMallResponseManage.fail(e.getCode(), e.getDesc());
        } catch (Exception e) {

            log.error("[PolyMallBiz][action]菠萝派mall系统异常：{}，请求：{}", e, request.toString());
            log.error("[PolyMallBiz][action]菠萝派mall系统异常" + ExceptionUtils.getRunException(e));
            return PolyMallResponseManage.fail(PolyErrorCodeEnum.SYSTEM_ERROR.getCode(), PolyErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }

        return PolyMallResponseManage.fail(PolyErrorCodeEnum.METHOD_ERROR.getCode(), PolyErrorCodeEnum.METHOD_ERROR.getDesc());
    }

}
